<template>
    <div class="main">
        <el-form :inline="true">
            <el-form-item>
                <el-input
                        v-model="id"
                    placeholder="ID"
                    clearable
                >
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="searchList">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button @click="showAll" type="warning">显示全部</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="restEdit(true)">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table
                :data="checked?editFormList:tableData"
                style="width: 100%;margin-bottom: 20px;"
                row-key="u_id"
                border
                default-expand-all
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column
                    prop="uid"
                    label="ID"
                    sortable
                    width="80">
            </el-table-column>
            <el-table-column
                    prop="username"
                    label="账号"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="password"
                    label="密码"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="姓名"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="hiredate"
                    label="入职时间"
                    width = "280">
            </el-table-column>
            <el-table-column
                    prop="gender"
                    label="性别"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="状态"
                    width="180"
            >
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status === '正编'" type="success">
                        正编
                    </el-tag>
                    <el-tag v-else-if="scope.row.status === '临时'" type="danger">
                        临时
                    </el-tag>
                    <el-tag v-else-if="scope.row.status === '聘用'" type="warning">
                        聘用
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="title"
                    label="职称"
                    width="180">

            </el-table-column>
            <el-table-column
                    prop="credit"
                    label="信誉度"
            width="220">
                <template slot-scope="scope">
                    <el-tag size="small" v-if="scope.row.credit===1" type="success">
                        优
                    </el-tag>
                    <el-tag size="small" v-else-if="scope.row.credit===0" type="danger">
                        差
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                    size="small"  @click="changeFormList(scope.$index,scope.row)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click.native="confirmDelete(scope.$index,scope.row)"
                            >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose">
            <el-form style="text-align: center" :model="editForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="ID" prop="u_id">
                    <el-input
                            placeholder="自动生成"
                            v-model="editForm.uid=null"
                            :disabled="true">
                    </el-input>
                </el-form-item>
                <el-form-item label="账号" prop="username">
                    <el-input v-model="editForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="editForm.password"></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="editForm.name"></el-input>
                </el-form-item>
                <el-form-item label="入职时间" prop="hiredate">
                    <el-col :span="11">
                        <el-date-picker
                                v-model="editForm.hiredate"
                                type="datetime"
                                placeholder="选择日期时间"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                align="right">
                        </el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                    <el-radio-group v-model="editForm.gender">
                        <el-radio label="男"></el-radio>
                        <el-radio label="女"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="editForm.status">
                        <el-radio label="正编"></el-radio>
                        <el-radio label="聘用"></el-radio>
                        <el-radio label="临时"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="职称" prop="title">
                    <el-input v-model="editForm.title"></el-input>
                </el-form-item>
                <el-form-item label="信誉度" prop="credit">
                    <el-radio-group v-model="editForm.credit">
                        <el-radio label="1"></el-radio>
                        <el-radio label="0"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-button  @click="restEdit(false)">取消</el-button>
                <el-button @click="restEdit(true)" type="warning">清空</el-button>
                <el-button  type="primary" @click="submitFormInsert(editForm)">确 定</el-button>
            </el-form>

        </el-dialog>


        <el-dialog
                title="提示"
                :visible.sync="change"
                width="30%"
                :before-close="handleClose">
            <el-form style="text-align: center" :model="changeForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="ID" prop="uid">
                    <el-input
                            v-model="changeForm.uid"
                            :disabled="true">
                    </el-input>
                </el-form-item>
                <el-form-item label="账号" prop="username">
                    <el-input v-model="changeForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="changeForm.password"></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="changeForm.name"></el-input>
                </el-form-item>
                <el-form-item label="入职时间" prop="hiredate">
                    <el-col :span="11">
                        <el-date-picker
                                v-model="changeForm.hiredate"
                                type="datetime"
                                placeholder="选择日期时间"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                align="right">
                        </el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                    <el-radio-group v-model="changeForm.gender">
                        <el-radio label="男"></el-radio>
                        <el-radio label="女"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="changeForm.status">
                        <el-radio label="正编"></el-radio>
                        <el-radio label="聘用"></el-radio>
                        <el-radio label="临时"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="职称" prop="title">
                    <el-input v-model="changeForm.title"></el-input>
                </el-form-item>
                <el-form-item label="信誉度" prop="credit">
                    <el-radio-group v-model="changeForm.credit">
                        <el-radio label="1"></el-radio>
                        <el-radio label="0"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-button  @click="resetChange(false)">取消</el-button>
                <el-button  type="primary" @click="submitFormUpdate">确 定</el-button>
            </el-form>

        </el-dialog>
        <div class="block">
            <el-pagination
                    background
                    layout="prev, pager, next"
                    :page-size="pageSize"
                    :total="total"
                    @current-change="page">
            </el-pagination>
        </div>
    </div>


</template>

<script>
    import Element from "element-ui"
    export default {
        inject:['reload'],
        name: "UserData",
        data() {
            return {
                pageSize: 1,
                total: 1,
                changeForm:{
                    uId:null,
                    uid: "",
                    username:"",
                    hiredate : null,
                    name: '',
                    gender: '',
                    status : '',
                    title : '',
                    credit: 1,
                    password:""
                },
                change:false,
                checked:false,
                editFormList:[],
                id:null,
                editForm:{
                    uId:null,
                    uid: "",
                    username:"",
                    hiredate : '',
                    name: '',
                    gender: '',
                    status : '',
                    title : '',
                    credit: 1,
                    password:""
                },
                rules: {
                    username: [
                        { required: true, message: '请输入账号', trigger: 'blur' },
                        { min: 4, max: 12, message: '长度在 4 到 12 个字符', trigger: 'blur' }
                    ],
                    password:[{ required: true, message: '请输入密码', trigger: 'change' }],
                    hiredate: [
                        {type: 'date', required: true, message: '请选择时间', trigger: 'change' }
                    ],
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'change' }
                    ],
                    gender: [
                        { required: true, message: '请选择性别', trigger: 'change' }
                    ],
                    status: [
                        { required: true, message: '请设置状态', trigger: 'change' }
                    ],
                    title: [
                        { required: true, message: '请设置职称', trigger: 'change' }
                    ],
                    credit: [
                        { required: true, message: '请设置信誉度', trigger: 'blur' }
                    ]
                },
                current:1,
                dialogVisible: false,
                tableData: [{
                    uId:null,
                    uid: 1,
                    username:"123456789",
                    password:"123123",
                    hiredate : '2021-05-16-12:00',
                    name: '王小虎',
                    gender: '男',
                    status : '正编',
                    title : '普通员工',
                    credit: 1
                }
                ]
            }
        },
        mounted() {
            this.getPage();
        },
        methods: {
            async page(currentPage){
                console.log(currentPage)
                this.$axios({
                    method:"post",
                    url:"http://localhost:80/user/getUserDataPage",
                    params:{
                        current:currentPage,
                        size:5
                    }
                }).then(res=>{

                    console.log(res)
                    this.pageSize = res.data.data.size
                    this.total = res.data.data.total
                    this.tableData = res.data.data.records
                })
                // console.log(res)
            },
            getPage(){
                this.$axios({
                    method:"post",
                    url:"http://localhost:80/user/getUserDataPage",
                    params:{
                        current:1,
                        size:5
                    }
                }).then(res=>{
                    console.log(res)
                    this.pageSize = res.data.data.size
                    this.total = res.data.data.total
                    this.tableData = res.data.data.records
                })
                // console.log(res)
            },
            searchList(){
                if (this.id!=null){
                    this.$axios({
                        method:"post",
                        url:"http://localhost:80/user/getUserById",
                        params:{
                            id: this.id
                        }
                    }).then( res =>{
                        this.editForm = res.data.data
                        this.editFormList=[]
                        this.editFormList.push(this.editForm)
                        this.checked = true
                        console.log(this.editForm)
                    })
                }else {
                    this.$message({
                        type:"error",
                        message:"请输入待查询ID"
                    })
                }
            },
            showAll(){
                this.checked = false
            },
            handleClose(done) {
                this.editForm = {
                    uId:null,
                    uid: null,
                    username:"",
                    hiredate : '',
                    name: '',
                    gender: '',
                    status : '',
                    title : '',
                    credit: null
                }
                done();
            },
            getUserData(){
                this.$axios({
                    method:"post",
                    url:"http://localhost/user/getUserData"
                }).then(res =>{
                    this.tableData = res.data.data
                    console.log(res);
                })
            },
            resetChange(e){
                this.change = e
                this.changeForm={}
            },
            restEdit(e){
                this.dialogVisible = e
                this.editForm =  {
                    uId:null,
                    uid: null,
                    username:"",
                    hiredate : '',
                    name: '',
                    gender: '',
                    status : '',
                    title : '',
                    credit: null
                }
            },
            changeFormList(index,row){
                this.change=true
                this.changeForm = row
                //console.log(this.changeForm.uid)
                this.changeForm.uId= this.changeForm.uid
                // this.changeForm.hiredate = null
                console.log(this.changeForm)
                if (this.changeForm.credit==="1"){
                    this.changeForm.credit ="1"
                }else {
                    this.changeForm.credit="0"
                }
            },
            submitFormUpdate(){
                this.$axios({
                    method:"post",
                    url:"http://localhost:80/user/update",
                    params:this.changeForm
                }).then(res=>{
                    this.getMessage(res.data.msg)
                    console.log(this.changeForm)
                    console.log(res)
                    if (res.data.msg === "更新成功"){
                        this.reload()
                    }
                })
            },
            submitFormInsert(e) {
                if (e.name!=null){
                    this.$axios({
                        method:"post",
                        url:"http://localhost/user/insert",
                        params:this.editForm
                    }).then(res =>{
                        console.log(res)
                        if (res.data.msg==="新增成功"){
                            this.getMessage("新增成功")
                            this.reload()
                        }
                        else {
                            this.getMessage(res.data.msg)
                        }
                    })
                }
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
            },
            handleDelete(index,row){
                console.log(index)
                console.log(row)
            },
            getMessage(msg){
                this.$message({
                    type: "success",
                    message: msg
                }
                )
            },
            confirmDelete(index,row) {
                let msg
                this.handleDelete(index,row)
                this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios({
                        method:"delete",
                        url:"http://localhost:80/user/deleteById",
                        params:{
                            id:row.uid
                        }
                    }).then(res =>{
                        console.log(res)
                        let code = res.data.code
                        if (code === 200){
                            this.getMessage(res.data.msg)
                            this.reload()
                        }
                        else {
                            this.getMessage(res.data.msg)
                            this.reload()
                        }

                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    }
</script>

<style scoped>
    .main{
        text-align: left;
    }
    .el-form{
        text-align: left;
    }
</style>
